(* Mathematica package *)

AbelesSIIv[pol_, omega_, thetalist_, nlist_, Dlist_, v_] /; 0 <= v < Length[Dlist] := 
 Dot @@ Riffle[
    RefractionMv[pol, thetalist[[v + 1 ;; -2]], thetalist[[v + 2 ;;]], nlist[[v + 1 ;; -2]], nlist[[v + 2 ;;]]], 
    PhaseMv[omega, thetalist[[v + 2 ;; -2]], nlist[[v + 2 ;; -2]], Dlist[[v + 1 ;;]]]
    ];

AbelesSIIv[pol_, omega_, thetalist_, nlist_, Dlist_, v_] /;v == Length[Dlist] := 
	RefractionMv[pol, thetalist[[v + 1]], thetalist[[v + 2]], nlist[[v + 1]], nlist[[v + 2]]];
	
AbelesSIIv[pol_, omega_, thetalist_, nlist_, Dlist_, v_] /;v == Length[Dlist] +1 := IdentityMatrix[2];


AbelesSIIv[pol_?PolQ, omega_,  ThetavNvDv : {{_, _}, {_, _, _} .., {_, _}}, v_Integer] /; 0 <= v < Length@ThetavNvDv - 2 :=
 Dot @@ Riffle[
 	RefractionMv[pol, ThetavNvDv[[v + 1 ;; -2, 1]], ThetavNvDv[[v + 2 ;;, 1]], ThetavNvDv[[v + 1 ;; -2, 2]], ThetavNvDv[[v + 2 ;;, 2]]],
 	PhaseMv[omega, Sequence @@ Transpose[ThetavNvDv[[v + 2 ;; -2]]]]]
AbelesSIIv[pol_?PolQ, omega_,  ThetavNvDv : {{_, _}, {_, _, _} .., {_, _}}, v_Integer] /; v == Length@ThetavNvDv - 2 := 
 RefractionMv[pol, Sequence @@ Flatten@Transpose[ThetavNvDv[[-2 ;;, 1 ;; 2]]]];
AbelesSIIv[pol_?PolQ, omega_,  ThetavNvDv : {{_, _}, {_, _, _} .., {_, _}}, v_Integer] /; v == Length@ThetavNvDv - 1 := IdentityMatrix[2];


AbelesSIIv[pol_?PolQ, omega_, Theta0_, n0_, NvDv:{{_,_}..}, nkp1_, v_Integer] /; 0 <= v < Length@NvDv :=
	Block[{ThetaLIST = {Theta0} ~Join~ RefractionAngle[Theta0, n0, NvDv[[All,1]]~Join~{nkp1}],
	           nLIST = {n0} ~Join~ NvDv[[All,1]] ~Join~ {nkp1}},
		Dot @@ Riffle[
 	RefractionMv[pol, ThetaLIST[[v + 1 ;; -2]], ThetaLIST[[v + 2 ;;]], nLIST[[v + 1 ;; -2]], nLIST[[v + 2 ;;]]],
 	PhaseMv[omega, ThetaLIST[[v + 2 ;; -2]], nLIST[[v + 2 ;; -2]], NvDv[[v + 1;;, 2]]]]]
 	
AbelesSIIv[pol_?PolQ, omega_, Theta0_, n0_, NvDv:{{_,_}..}, nkp1_, v_Integer] /; v == Length@NvDv := 
	RefractionMv[pol, Sequence @@ RefractionAngle[Theta0, n0, {NvDv[[-1,1]], nkp1}], NvDv[[-1,1]], nkp1];
AbelesSIIv[pol_?PolQ, omega_, Theta0_, n0_, NvDv:{{_,_}..}, nkp1_, v_Integer] /; v > Length@NvDv := IdentityMatrix[2];


(*Error handling for bad arguments*)
AbelesSIIv[___]/;Message[General::badargs,AbelesSIIv]:="dummy result, so symbol returned unevaluated";
